import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_dropdown_selection.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/task/base_danger_investigation_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/hazard_investigation/hazard_management/hazard_management_controller.dart';

/// 我的任务-隐患排查-隐患治理页面
class HazardManagementPage extends StatelessWidget {
  HazardManagementPage({super.key});

  final controller = Get.put(HazardManagementController());

  ///获取条件搜索项
  Widget _getSearchConditionWidget() {
    return Row(
      children: [
        Expanded(
            child: CustomSearchEnter(
          hintText: '输入任务名称',
          height: 38.w,
          text: controller.search,
          onCallBack: (value) {
            controller.search = value;
            controller.refreshPage();
          },
        )),
        AppGaps.hGap8,
        SizedBox(
          width: 80.w,
          height: 38.w,
          child: CustomDropdownSelection(
            hintText: '筛选',
            onCallBack: () {
              controller.screen();
            },
          ),
        ),
        // _getFilter()
      ],
    );
  }

  /// 获取隐患状态（待确认：-1；待整改：0；待验收：1；已验收：9；驳回：10）
  Widget _getStateView(String type) {
    String stateText = '';
    Color? color;
    switch (type) {
      case '-1':
        stateText = '待确认';
        color = const Color(0xffFC9900);
        break;
      case '0':
        stateText = '待整改';
        color = const Color(0xffFFCE24);
        break;
      case '1':
        stateText = '待验收';
        color = const Color(0xff5C6DFF);
        break;
      case '9':
        stateText = '已验收';
        color = const Color(0xff73D13D);
        break;
      case '10':
        stateText = '驳回';
        color = const Color(0xffFD6B6B);
        break;
      default:
        break;
    }
    if (stateText.isEmpty) {
      return Container();
    }
    return Container(
      height: 18.w,
      width: 52.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(8.w),
          topRight: Radius.circular(8.w),
          bottomRight: Radius.circular(8.w),
        ),
      ),
      child: Text(
        stateText,
        style: TextStyle(
          color: const Color(0xffFFFFFF),
          height: 1.2,
          fontWeight: FontWeight.w400,
          fontSize: 10.sp,
        ),
      ),
    );
  }

  /// 获取列表对象-单元-事件显示
  String _getText(BaseDangerInvestigationEntity item) {
    return '${item.riskObjectName}${item.riskUnitName == '' ? '' : '/${item.riskUnitName}'}${item.riskEventName == '' ? '' : '/${item.riskEventName}'}';
  }

  /// 列表任务展示项
  Widget _getTaskView(BaseDangerInvestigationEntity item) {
    return Container(
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(8.w))),
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 13.w),
            child: Row(
              children: [
                SvgPicture.asset(
                  AppImages.icon_title,
                  width: 20.w,
                  height: 20.w,
                ),
                AppGaps.hGap6,
                Expanded(
                    child: Text(
                  item.dangerRecordDesc!,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: const Color(0xff262F38),
                    height: 1.2,
                    fontWeight: FontWeight.w500,
                    fontSize: 15.sp,
                  ),
                )),
                AppGaps.vGap6,
                _getStateView(item.dangerState!)
              ],
            ),
          ),
          Container(
            height: 1.w,
            color: const Color(0xff587DF5).withOpacity(0.2),
          ),
          Container(
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Visibility(
                    visible: item.liableDate!.isNotEmpty,
                    child: Row(
                      children: [
                        Text(
                          '整改时间：',
                          style: TextStyle(color: const Color(0xffA5A6B5), height: 1.2, fontWeight: FontWeight.w400, fontSize: 12.sp),
                        ),
                        Expanded(
                          child: Text(
                            item.liableDate!,
                            style: TextStyle(color: const Color(0xff495061), height: 1.2, fontWeight: FontWeight.w400, fontSize: 12.sp),
                          ),
                        )
                      ],
                    )),
                Visibility(
                  visible: _getText(item).isNotEmpty,
                  child: Column(
                    children: [
                      AppGaps.vGap10,
                      Text(
                        _getText(item),
                        style: TextStyle(
                          color: const Color(0xff495061),
                          height: 1.2,
                          fontWeight: FontWeight.w400,
                          fontSize: 12.sp,
                        ),
                      ),
                    ],
                  ),
                ),
                Visibility(
                    visible: item.riskMeasureDesc!.isNotEmpty,
                    child: Column(
                      children: [
                        AppGaps.vGap10,
                        Row(
                          children: [
                            Text(
                              '管控措施：',
                              style: TextStyle(color: const Color(0xffA5A6B5), height: 1.2, fontWeight: FontWeight.w400, fontSize: 12.sp),
                            ),
                            Expanded(
                              child: Text(
                                item.riskMeasureDesc!,
                                style: TextStyle(color: const Color(0xff495061), height: 1.2, fontWeight: FontWeight.w400, fontSize: 12.sp),
                              ),
                            )
                          ],
                        )
                      ],
                    )),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.popRightScaffoldKey,
            appBar: CustomAppBar(
              titleText: '隐患治理',
            ),
            // 右侧筛选栏
            endDrawer: Container(
              width: AppDimens.sw - 70.w,
              height: AppDimens.sh,
              color: Colors.white,
              child: CustomFilterRightSheet(
                dataList: controller.filterList,
                onConfirm: (List<FilterBean> value) {
                  // filterList = value;
                  // for (var i in value) {
                  //   if (i.title == '作业类型') {
                  //     selectType = i.chosenValue;
                  //   }
                  //   if (i.title == '作业状态') {
                  //     selectStateType = i.chosenValue;
                  //   }
                  // }
                  // specialPageController!.update();
                  // refreshPage();
                },
              ),
            ),
            body: Container(
              color: const Color(0xffF5F5F6),
              padding: EdgeInsets.symmetric(horizontal: 10.w),
              child: Column(
                children: [
                  AppGaps.vGap10,
                  _getSearchConditionWidget(),
                  AppGaps.vGap16,
                  Expanded(
                    child: CustomEasyRefreshList<BaseDangerInvestigationEntity>(
                      key: controller.globalKey,
                      dataLoad: (pageIndex, pageSize, {String? search}) async {
                        return controller.getListData(pageIndex, pageSize);
                      },
                      itemBuilder: (BaseDangerInvestigationEntity item, int index) {
                        return _getTaskView(item).clickOnce(() {
                          controller.clickOnTask(item);
                        });
                      },
                    ),
                  )
                ],
              ),
            ),
          );
        });
  }
}
